const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {   
        time:0,
        showAdver:true,
        interval:"",
        showAdTime:false,
        timers:0
       },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad:function(options) {
        var that=this;
        console.log('----login----options----')
        console.log(options);
        console.log('-----login---options----');
      
        var enterOptions =wx.getEnterOptionsSync();
        console.log(enterOptions);
        var path = enterOptions.path;
        if(path == "pages/login/index"){
            path="pages/index/index";
        }
        this.setData({
            path:path
        });
        console.log(path);
        var query=enterOptions.query;
        if(Object.keys(query).length === 0){
            console.log('----share index----query----');
            
        }else{
            console.log(query)
            if(query.hasOwnProperty('parent_id')){
                console.log('---has parent_id---');
               //var prquery.parent_id);
               this.setData({
                parent_id:query.parent_id
              })
              wx.setStorageSync('parent_id',query.parent_id);
            }
            if(query.hasOwnProperty('store_id')){
                console.log('---has store_id---');
                 this.setData({
                    store_id:query.store_id
                  })
                  wx.setStorageSync('store_id',query.store_id);
            }
            if(query.hasOwnProperty('help')){
                console.log('---has help---');
                this.setData({
                    help:query.help
                  })
                  wx.setStorageSync('help',query.help);
            }
            //console.log('none');
        }
        console.log('----login----enterOptions----');
        var date=util.formatTime(new Date());
        log.info("Time:"+date+",page:login,options:"+JSON.stringify(enterOptions));
       this.getLoginImage();
       that.wxUpdate(); //that.wxLogIn();  
     
    },
    wxUpdate:function(){
      let that=this;
      console.log('wxLogin')
      var tabArray=[ 
        {
        custom: true,
        "color": "#515151",
        "selectedColor": "#d81e06",
        "backgroundColor": "#F7F7F7",
        list: [ 
            {
                "pagePath": "pages/index/index",
                "text": "购车门店",
                "iconPath": "/icon/store_gary.png",
                "selectedIconPath": "/icon/store.png"
            },
            {
                "pagePath": "pages/mine/index",
                "text": "我的",
                "iconPath": "/icon/mine_gary.png",
                "selectedIconPath": "/icon/mine.png"
            }
    ]
    },
    {
      custom: true,
      "color": "#515151",
     "selectedColor": "#d81e06",
     "backgroundColor": "#F7F7F7",
     
      list: [
        {
          "pagePath": "pages/store/detail",
          "text": "店铺",
          "iconPath": "/icon/active_gary.png",
          "selectedIconPath": "/icon/active.png"
      },
      {
        "pagePath": "pages/coupon/index",
        "text": "促销",
        "iconPath": "/icon/index_gary.png",
        "selectedIconPath": "/icon/index.png"
    },
    {
        "pagePath": "pages/coupon/coupon",
        "text": "膨胀券",
        "iconPath": "/icon/coupon_gary.png",
        "selectedIconPath": "/icon/coupon.png"
    },
      {
          "pagePath": "pages/mine/index",
          "text": "我的",
          "iconPath": "/icon/mine_gary.png",
          "selectedIconPath": "/icon/mine.png"
      }
    ]
    }
    ,
    {
      custom: true,
      "color": "#515151",
     "selectedColor": "#d81e06",
     "backgroundColor": "#F7F7F7",
     
      list: [
        {
          "pagePath": "pages/store/detail",
          "text": "店铺",
          "iconPath": "/icon/active_gary.png",
          "selectedIconPath": "/icon/active.png"
      },
      {
        "pagePath": "pages/coupon/index",
        "text": "促销",
        "iconPath": "/icon/index_gary.png",
        "selectedIconPath": "/icon/index.png"
    },
    {
        "pagePath": "pages/coupon/coupon",
        "text": "膨胀券",
        "iconPath": "/icon/coupon_gary.png",
        "selectedIconPath": "/icon/coupon.png"
    },
      {
          "pagePath": "pages/mine/index",
          "text": "我的",
          "iconPath": "/icon/mine_gary.png",
          "selectedIconPath": "/icon/mine.png"
      }
    ]
    }
    ];
    wx.setStorageSync('tabBar',tabArray)
      that.wxLogin();    
     },
    wxLogin:function(){
      //获取个人资料缓存
      var open=wx.getStorageSync('open');
       console.log(open);
       //var date=util.formatTime(new Date());
       if(open){
        var unionId = open.unionid;
        //var userNickName = open.user_nicknInfo;
        var user_id = open.user_id;
        var open_id = open.id;
        var parent_id = wx.getStorageSync('parent_id');
        if(parent_id==undefined){
            parent_id=0;
          }
          if(parent_id==''){
            parent_id=0;
          }
        var store_id=wx.getStorageSync('store_id');
        if(store_id==undefined){
          store_id=0;
        }
        if(store_id==''){
          store_id=0;
        }
       // id=undefined;
        let that=this;
        wx.request({
          url: app.globalData.platUrl+'login/getWechatOpen',
          data:{id:open_id,from:3,unionid:unionId,parent_id:parent_id,store_id:store_id},
          success:res=>{        
            console.log(res);
            if(res.statusCode==200){
                console.log(res.statusCode);
              var unionid=res.data.unionid;
              app.globalData.unionid=unionid;
              var roles=res.data.roles;
              wx.setStorageSync('role',roles); 
           //  wx.setStorageSync('user',res.data.user); 
              wx.setStorageSync('open',res.data.open); 
             that.showAdvert(); 
            }else{
                console.log(res.statusCode);
              that.getUnionId();
            }                             
          }
        })   
       }else{
           this.getUnionId(); 
        }
    },
    getUnionId:function(){
      var that=this;
      var open=wx.getStorageSync('open');
      var parent_id=wx.getStorageSync('parent_id');
      var store_id=wx.getStorageSync('store_id');
      if(store_id==undefined){
        store_id=0;
      }
      if(store_id==''){
        store_id=0;
      }
     wx.login({
        success:res=>{
          console.log(res)
          var code =res.code;
         wx.request({
                url: app.globalData.platUrl+'login/wechatLogin',
                data:{code:code,parent_id:parent_id,store_id:store_id},
                success:function(res){    
                    console.log(res);     
                 let data = res.data;   
                 var unionid = data.unionid;   
                  
                 if(res.statusCode==200){
                   wx.setStorageSync('open',data.open); 
                  // wx.setStorageSync('user',data.user); 
                   wx.setStorageSync('role', data.roles)                             
                   that.showAdvert();              
                 }
      
                }
              })
            
          
        }
      })
    },
    getLoginImage:function(){
        let that = this;
        wx.request({
          url: app.globalData.platUrl+'login/getLoginImage',
          data:{from:1},
          success:res=>{
            that.setData({
              loginImage:res.data.image
            })
          }
      })
      },
      showAdvert:function(){
        var parent_id=wx.getStorageSync('parent_id');
        var store_id=wx.getStorageSync('store_id');
        var path = this.data.path;
        console.log(path);
        console.log('store_id='+store_id)
        if(store_id==undefined || store_id =='' || store_id ==0){
            store_id=0; 
            var url = '/'+path+'?parent_id='+parent_id+'&store_id='+store_id;          
            console.log(url);
                wx.reLaunch({
                    url: url,
                });
        }else{
          path="pages/coupon/index";
          wx.setStorageSync("tabIndex",1);
          var url = '/'+path+'?parent_id='+parent_id+'&store_id='+store_id;
          wx.reLaunch({             
            url: url,
          });
        }
          
    
        this.setData({
          showAdTime:true
        });
        let that = this;
        var timers;
        //that.data.timers = setInterval(that.toTimer,1000);   
  
       　
    },
    toTimer:function(){
        var time=this.data.time;
        let that=this;
       // console.log(this.data.timers)
        if(time>=0){
            clearInterval(that.data.timers);
          //  console.log(time);        
            this.setData({
               time:0
            });
            wx.reLaunch({
                url: '../index/index',
              })
        }else if(time > 3){
            clearInterval(that.data.timers);
        }else {  
            time = time + 1;         
            //console.log(time);
            this.setData({
                time:time
            }) 
        }
    },
         
   cliadv: function() {
       　　clearInterval(this.data.timers)
       　   this.setData({
               showAdver:false,
               time:9
             })
             wx.reLaunch({
             url: '../index/index',
           })　
       },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        clearInterval(this.data.timers);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.data.timers);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})